package org.dreamwork.cai.srf;

import org.dreamwork.cai.SRFException;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * 服务路由帧 {@code SRF}.
 * <p>用于描述一个远程的服务提供节点</p>
 *
 */
public class ServiceRouteFrame implements Serializable {
    public final String namespace;
    public final int port;
    public final Map<String, ServiceRouteItem> items = Collections.synchronizedMap (new HashMap<> ());

    public int udpPort;
    public String host;
    public int metric = 0;
    public long timestamp = System.currentTimeMillis ();
    public String description;

    public ServiceRouteFrame (String namespace, String host, int port) {
        this.namespace = namespace;
        this.host      = host;
        this.port      = port;
    }

    public void add (ServiceRouteItem item) throws SRFException {
        if (items.containsKey (item.name)) {
            throw new SRFException ("item named " + item.name + " already exists!");
        }

        items.put (item.name, item);
    }

    public ServiceRouteItem get (String name) {
        return items.get (name);
    }
}
